package com.qf.service.shangpin_service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Channel;
import com.qf.bean.Commodity;
import com.qf.dao.shangpin_dao.ChDao;
import com.qf.dao.shangpin_dao.CommDao;
import com.qf.service.shangpin_service.CommService;
import com.qf.utils.Page_shangpin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;

/**
 * 10/12/2019
 * Echo
 * sassspringboot
 * Alibaba
 */
@Service
public class CommServiceImpl implements CommService {


    @Autowired
    private CommDao commDao;


    @Override
    public PageInfo<Commodity> show(Integer index, Commodity commodity) {
        HashMap map = new HashMap();
        map.put("index", index);
        map.put("commname", commodity.getCommName());
        map.put("tid", commodity.getCommTypeId());
        map.put("bid", commodity.getBrandId());
        map.put("status", commodity.getStatus());
        PageHelper.startPage(index, Page_shangpin.PAGESIZE.getSize());
        List<Commodity> commlist = commDao.show(map);
        PageInfo<Commodity> pageInfo = new PageInfo(commlist);
        return pageInfo;
    }

    @Override
    @Transactional
    public Integer add(Commodity commodity,int[] ch) {
        Integer i=commDao.add(commodity);
        if (ch!=null){
            HashMap map = new HashMap();
            double price=commodity.getCbPrice()*0.3+commodity.getCbPrice();
            map.put("price",price);
            double cjPrice=commodity.getCbPrice()*0.5+commodity.getCbPrice();
            map.put("cjPrice",cjPrice);
            map.put("commid",commodity.getId());
            map.put("chs",ch);
            Integer j= commDao.addMid(map);
        }
        return i;
    }

    @Override
    public Commodity findByCommId(Integer commId) {
       Commodity commodity= commDao.findByCommId(commId);
       return commodity;
    }

    @Override
    @Transactional
    public Integer update(Commodity commodity) {
        //删除中间表中数据
//       Integer i= commDao.deleteMid(commodity.getId());
       //修改商品表
       Integer j= commDao.update(commodity);
       //重新在中间表中添加数据
//        HashMap map = new HashMap();
//        double price=commodity.getCbPrice()*0.3+commodity.getCbPrice();
//        map.put("price",price);
//        double cjPrice=commodity.getCbPrice()*0.5+commodity.getCbPrice();
//        map.put("cjPrice",cjPrice);
//        map.put("commid",commodity.getId());
//        map.put("chs",ch);
//        Integer b=commDao.addMid(map);
        return j;
    }

    @Override
    public PageInfo<Commodity> chuxiaoShow(Integer index, String commName,Integer brandId, Integer commTypeId, Integer status) {
        HashMap map = new HashMap();
        map.put("brandId",brandId);
        map.put("commTypeId",commTypeId);
        map.put("status",status);
        map.put("commName",commName);
        PageHelper.startPage(index,Page_shangpin.PAGESIZE.getSize());
        List<Commodity> commodityList=commDao.chuxiaoShow(map);
        PageInfo<Commodity> pageInfo = new PageInfo<>(commodityList);
        return pageInfo;
    }

    @Autowired
    private ChDao chDao;
    @Override
    @Transactional
    public Integer addCh(Integer commId, double cbPrice, String cname) {
        Channel channel = new Channel();
        channel.setCname(cname);
        Integer i= chDao.add(channel);
        HashMap map = new HashMap();
        double price=cbPrice*0.3+cbPrice;
        map.put("price",price);
        double cjPrice=cbPrice*0.5+cbPrice;
        map.put("cjPrice",cjPrice);
        map.put("commid",commId);
        map.put("chId",channel.getChannelId());
        Integer j= commDao.addMid2(map);
        return j;


    }

    @Override
    public List<Commodity> findByIds(int[] ids) {
       return commDao.findByIds(ids);

    }
}
